package com.itany.jdbc.jdbc;

import com.itany.jdbc.execption.DataAccessExecption;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author 陈华旺
 * @version 1.0
 * @date 2023年10月27日 9:21
 * @Description 事务控制器
 */
public class TransactionManager {
    public void begin(){
        try {
            Connection conn = JDBCUtil.getConn();
            conn.setAutoCommit(false);
        } catch (SQLException e) {
            throw new DataAccessExecption("transaction open failed:",e);
        }
    }
    public void commit(){
        try {
            Connection conn = JDBCUtil.getConn();
            conn.commit();
        } catch (SQLException e) {
            throw new DataAccessExecption("transaction commit failed:",e);
        } finally {
            JDBCUtil.close();
        }
    }

    public void rollback(){
        try {
            Connection conn = JDBCUtil.getConn();
            conn.rollback();
        } catch (SQLException e) {
            throw new DataAccessExecption("transaction rollback failed:",e);
        } finally {
            JDBCUtil.close();
        }
    }
}
